<?php
/**
*
* @package ppkBB3cker
* @version $Id: new_torrents_add1.php 1.000 2010-10-30 18:39:00 PPK $
* @copyright (c) 2010 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$new_torrents='';
$ppkbb_newtorrents_exclude=$r_torrents=$torrents_posters=array();
if($config['ppkbb_newtorrents_exclude'])
{
	$ppkbb_newtorrents_exclude=my_split_config($config['ppkbb_newtorrents_exclude'], 0, 'my_int_val', ',');
}
$disallow_access=array_unique(array_keys($auth->acl_getf('!f_read', true)));
$config['ppkbb_newtorrents_trueexclude'] ? $ppkbb_newtorrents_exclude=array_unique(array_merge($ppkbb_newtorrents_exclude, $disallow_access)) : '';

$sql="SELECT a.attach_id, a.post_msg_id, a.extension, a.real_filename, p.post_subject, a.physical_filename, p.topic_id, p.forum_id, t.times_completed, t.leechers, t.seeders, t.id torrent_id FROM ".ATTACHMENTS_TABLE." a, ".POSTS_TABLE." p, ". TRACKER_TORRENTS_TABLE ." t WHERE ".($ppkbb_newtorrents_exclude ? " p.forum_id ".($config['ppkbb_newtorrents_trueexclude'] ? 'NOT '  : '')."IN('".implode("', '", $ppkbb_newtorrents_exclude)."') AND " : '').($config['ppkbb_newtorrents_trueexclude'] ? '' : " p.forum_id NOT IN('".implode("', '", $disallow_access)."') AND ").($new_torrents_fid ? " p.forum_id={$new_torrents_fid} AND " : '')."a.post_msg_id=p.post_id AND t.topic_id=p.topic_id AND a.extension!='torrent' ".($ppkbb_new_torrents[7] ? "AND t.seeders >= {$ppkbb_new_torrents[7]} " : '')."AND a.real_filename LIKE '%poster%' ORDER BY t.times_completed DESC LIMIT 0, {$ppkbb_new_torrents[3]}";
$result=$db->sql_query($sql, $ppkbb_new_torrents[6]);
$image_ext=array('gif', 'jpeg', 'jpg', 'png', 'tga', 'tif', 'tiff');
while($row=$db->sql_fetchrow($result))
{
	if(in_array($row['extension'], $image_ext))
	{
		$torrents_posters[$row['torrent_id']]=$row;
		$r_torrents[$row['torrent_id']]=$row['torrent_id'];
	}
}
$db->sql_freeresult($result);

$r_peers=array();
if(!isset($ppkbb_tcenable_rannounces))
{
	$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');
}
if($ppkbb_tcenable_rannounces[0] && sizeof($r_torrents))
{
	$sql="SELECT torrent, SUM(seeders) seeders, SUM(leechers) leechers, SUM(peers) peers, SUM(times_completed) times_completed FROM ".TRACKER_RANNOUNCES_TABLE." WHERE torrent IN('".(implode("', '", $r_torrents))."') GROUP BY torrent";
	$result=$db->sql_query($sql, $ppkbb_new_torrents[6]);
	while($row=$db->sql_fetchrow($result))
	{
		$r_peers[$row['torrent']]=$row;
	}
}

foreach($torrents_posters as $k => $v)
{
	$t_title=$v['post_subject']. " - {$user->lang['TORRENT_COMPLETED']}: ".($v['times_completed']+my_int_val(@$r_peers[$k]['times_completed']))." {$user->lang['TORRENT_SEEDERS']}: ".($v['seeders']+my_int_val(@$r_peers[$k]['seeders']))." {$user->lang['TORRENT_LEECHERS']}: ".($v['leechers']+my_int_val(@$r_peers[$k]['leechers']));
	$new_torrents.='&nbsp;<a href="'.append_sid($phpbb_root_path.'viewtopic.' . $phpEx . '?t=' . $v['topic_id'] . '&amp;f=' . $v['forum_id']).'" title = "'.$t_title.'" ><img src="'.append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $v['attach_id']).'" alt="'.$t_title.'" height="'.$ppkbb_new_torrents[5].'" /></a>&nbsp;';
}

$template->assign_vars(array(
	'NEW_TORRENTS_POSTERS' => $new_torrents ? $new_torrents : false,
	'S_NEW_TORRENTS' => $new_torrents_fid && @$forum_data['forum_name'] ? sprintf($user->lang['NEW_TORRENTS_INFORUM'], $ppkbb_new_torrents[3], $forum_data['forum_name']) : sprintf($user->lang['NEW_TORRENTS'], $ppkbb_new_torrents[3]),
	'S_NEW_TORRENTS_WIDTH' => $ppkbb_new_torrents[4],
	'S_NEW_TORRENTS_HEIGHT' => $ppkbb_new_torrents[5],
	)
);

?>
